import time

file_name = 'b.txt'
counter = 0
def write_file1():
    global counter
    with open(file_name, 'a+',encoding='utf-8') as f:
        f.write(str(counter))
        counter = counter + 1
        print(counter)
        # 无需关闭文件

def write_file2():
    global counter
    f = open('c.txt', 'a+',encoding='utf-8')
    f.write(str(counter))
    counter = counter + 1
    print(counter)
    # f.close()
 

# while counter < 10:
#     write_file1()
#     time.sleep(1)




# 高频调用 write_file1（无问题）
for _ in range(100000):
    write_file1()

# 高频调用 write_file2（约 10 万次后崩溃）
# for _ in range(100000):
#     write_file2()

print("done")


